﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Legazpi.Payroll.Web.Models;
using Newtonsoft.Json;
using System.Text;
using Emz.Core.Models;
using Ninject;
using Legazpi.Payroll.Services.Interfaces;
using Legazpi.Payroll.Web.Helpers;
using Emz.Core.Models.Enums;

namespace Legazpi.Payroll.Web.Admin.Payroll
{
    public partial class DailyTimeRecords : System.Web.UI.Page
    {

        [Inject]
        public IPayrollDateService _payrollDateService { get; set; }
        [Inject]
        public IEmployeeService _employeeService { get; set; }
        [Inject]
        public IDayTimeRecordService _dayTimeRecordService { get; set; }


        protected int CurrentPayrollDateId = 0;
        protected int StartDay = 1;
        protected int EndDay = 16;
        protected int ColumnDaySpan = 0;
        protected int FooterColumnSpan = 0;
        protected static string TimeInID = "TimeIn";
        protected static string TimeOutID = "TimeOut";
        protected static string TotDayHourID = "TotDayHour";
        protected static string DDHoliday = "DDHoliday";
        protected static string RegWorkHours = "RegWorkHours";
        protected static string SaveBtn = "SaveBtn";
        protected static string CalcBtn = "CalcBtn";
        protected static string EmpTotalAmt = "EmpTotalAmt";
        protected static string DTRKey = "DTRKey"; 
        protected string ArrayEmployeeId = "";
        protected List<EmployeeDTR> Model;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //Clients/Deployed At            
                DeployedDropdown.ToSectorSelectList(0);
            }
            
            var currentPayrollDate = _payrollDateService.GetActivePayroll();
            StartDay = currentPayrollDate.Startdate.Day;
            EndDay = currentPayrollDate.Enddate.Day;            
            CurrentPayrollDateId = currentPayrollDate.Id;

            literalPayrollDateCovered.Text = string.Format("{0} - {1}", currentPayrollDate.Startdate.ToString("MMM. dd, yyyy"), currentPayrollDate.Enddate.ToString("MMM. dd, yyyy"));

            ColumnDaySpan = (EndDay - StartDay) + 1;
            FooterColumnSpan = ColumnDaySpan + 2;
                        
            var dataList = _employeeService.GetAll()
                .Where(x => x.DetachmentId == int.Parse(DeployedDropdown.SelectedValue))
                .Select(employee => 
                    EmployeeDTR.GetDTO(_dayTimeRecordService.GetEmployeeDTRbyPayroll(employee, currentPayrollDate))).ToList();
            Model = dataList;
                                           
            dtrRepeater.DataSource = dataList;
            dtrRepeater.DataBind();

            //Register to client all employeeIds
            StringBuilder sb = new StringBuilder();
            dataList.ForEach(dtr => sb.Append(string.Format("{0},", dtr.EmployeeId)));
            if (sb.Length > 0)            
                ArrayEmployeeId = sb.ToString().TrimEnd(',');


            
        }

        private string GetHolidaySelection(int employeeId,int iDay,List<TimeRecord> timeRecords)
        { 
            var sb = new StringBuilder();
            string strInputID = string.Format("{0}_{1}_{2}", DDHoliday, iDay, employeeId);
            var timeRecord = timeRecords.Where(x => x.Day == iDay).FirstOrDefault();
            int selHoliday = timeRecord != null ? timeRecord.Holiday : 0;
            sb.AppendLine("<td>");
            sb.AppendLine(string.Format("<select id=\"{0}\" >", strInputID));
            sb.AppendLine(string.Format("  <option value=\"{0}\">- - -</option>", (int)HolidayType.None));
            sb.AppendLine(string.Format("  <option value=\"{0}\" {1}>LGL</option>", (int)HolidayType.Legal, selHoliday == (int)HolidayType.Legal ? "selected=\"selected\"" : string.Empty));
            sb.AppendLine(string.Format("  <option value=\"{0}\" {1}>SPC</option>", (int)HolidayType.Special, selHoliday == (int)HolidayType.Special ? "selected=\"selected\"" : string.Empty));                 
            sb.AppendLine(string.Format("</select>"));
            sb.AppendLine("</td>");
            return sb.ToString();                   
        }
        protected void dtrRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var columnDataTimeIn = e.Item.FindControl("ColumnDataTimeIn") as Literal;
                var columnDataTimeOut = e.Item.FindControl("ColumnDataTimeOut") as Literal;
                var columnDataHours = e.Item.FindControl("ColumnDataHours") as Literal;
                var columnSelectedHoliday = e.Item.FindControl("ColumnSelectedHoliday") as Literal;
                

                var employeeId = ((EmployeeDTR)e.Item.DataItem).Id;
                for (int iDay = StartDay; iDay <= EndDay; iDay++)
                {
                    string strTimeInInputID = string.Format("{0}_{1}_{2}", TimeInID, iDay, employeeId);
                    string strTimeOutInputID = string.Format("{0}_{1}_{2}", TimeOutID, iDay, employeeId);
                    string strTotDayHourInputID = string.Format("{0}_{1}_{2}", TotDayHourID, iDay, employeeId);

                    string strTimeInInputValue = ((EmployeeDTR)e.Item.DataItem).TimeRecords.GetTimeDisplay(iDay,true);
                    string strTimeOutInputValue = ((EmployeeDTR)e.Item.DataItem).TimeRecords.GetTimeDisplay(iDay, false);
                    string strTotDayHourInputValue = ((EmployeeDTR)e.Item.DataItem).TimeRecords.GetNoOfHours(iDay).ToString();

                    columnDataTimeIn.Text += string.Format("<td><input type=\"text\" id=\"{0}\" value=\"{1}\" class=\"inputTime\"  /></td>", strTimeInInputID, strTimeInInputValue);
                    columnDataTimeOut.Text += string.Format("<td><input type=\"text\" id=\"{0}\" value=\"{1}\" class=\"inputTime\"  /></td>", strTimeOutInputID, strTimeOutInputValue);
                    columnDataHours.Text += string.Format("<td><input type=\"text\" id=\"{0}\" value=\"{1}\" class=\"inputTotHrs\" readonly=\"readonly\" /></td>", strTotDayHourInputID, strTotDayHourInputValue);
                    columnSelectedHoliday.Text += GetHolidaySelection(employeeId, iDay, ((EmployeeDTR)e.Item.DataItem).TimeRecords);
                }
            }
        }

    }
}